package com.apes.hr.salary.rewardPunish.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：集体奖惩记录单
 * @author linwz
 * @create 2019-2-27 08:47
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TeamRewardPunish.CONST_TABLE_NAME)
public class TeamRewardPunish extends Model {
    public static final String CONST_TABLE_NAME = "hr_Team_Reward_Punish";

    @Id
    @Column(name = "id")
    @Comment("奖惩记录单号")
    private String id;   //奖惩记录单号


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reward_punish_type_id", referencedColumnName = "id")
    @Comment("奖惩类型")
    private RewardPunishType rewardPunishType;  // 奖惩类型

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "quota_type", referencedColumnName = "id" )
    @Comment("指标类别")
    private Enumeration quotaType;//指标类别


    @Column(name = "explain", length = 1000)
    @Comment("具体情况说明")
    private String explain;    // 具体情况说明

    @Column(name = "doc_number", length = 32)
    @Comment("奖罚文号")
    private String docNumber;   // 奖罚文号

    @Column(name = "doc_file_name", length = 32)
    @Comment("文件名")
    private String docFileName;   // 文件名

    @Column(name = "state", length = 10, nullable = false)
    @Comment("状态")
    private String state; //状态： ('sent', '提交'), ('done', '已审核')

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid; //审批人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "send_out_uid", referencedColumnName = "id")
    @Comment("发文人")
    private PartyRole sendOutUid; //发文人

    @Column(name = "send_out_date")
    @Comment("发文日期")
    private Date sendOutDate; //发文日期

    @Column(name = "reward_date")
    @Comment("事发日期起")
    private Date rewardDate; //事发日期起

    @Column(name = "reward_end_date")
    @Comment("事发日期止")
    private Date rewardEndDate; //事发日期止

    @Column(name = "cash_date")
    @Comment("兑现日期")
    private Date cashDate; //兑现日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cancel_uid", referencedColumnName = "id")
    @Comment("作废人")
    private PartyRole cancelUid; //作废人

    @Column(name = "cancel_date")
    @Comment("作废日期")
    private Date cancelDate; //作废日期

    @Column(name = "amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("奖励总额")
    private double amt = 0;    //奖励总额

    @Column(name = "punish_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("处罚总额")
    private double punishAmt = 0;    //处罚总额

    @Column(name = "total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("合计")
    private double total = 0;//合计

    @OneToMany(mappedBy = "teamRewardPunish", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<TeamRewardPunishItem> teamRewardPunishItems = new HashSet(); //奖罚单行

    public Set<TeamRewardPunishItem> getTeamRewardPunishItems() {
        if (teamRewardPunishItems==null||teamRewardPunishItems.isEmpty())return new HashSet<>();
        for (TeamRewardPunishItem item : teamRewardPunishItems) {
            item.setTeamRewardPunish(this);
        }
        return teamRewardPunishItems;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept; // 部门(用于部门只能查询本部门数据)

}
